package com.easthotech.controller;


import com.easthotech.property.WeChatProperty;
import com.easthotech.util.HttpUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;

@Controller
public class UserController {



    @Autowired
    private WeChatProperty weChatProperty;


    /**
     * 获取应用Token
     * @param corpsecret
     * @return
     * @throws IOException
     */
    public String getAccessToken(String corpsecret) throws IOException {
        String corpid = weChatProperty.getCorpid();

        //String corpsecret = weChatProperty.getCorpsecrets().get("message");


        String url = weChatProperty.getUrls().get("gettoken");

        HashMap<String, String> map = new HashMap<>();

        map.put("corpid",corpid);
        map.put("corpsecret",corpsecret);

        String result = HttpUtil.get(url, map);


        ObjectMapper mapper = new ObjectMapper();

        JsonNode jsonNode = mapper.readTree(result);

        String access_token = jsonNode.get("access_token").asText();
        System.out.println("access_token =" + access_token);

        System.out.println("access_token = "+access_token);

        return access_token;
    }


    /**
     * 发送消息
     * @param userID
     * @param deptID
     * @param content
     * @return
     * @throws Exception
     */
    @RequestMapping("/send")
    @ResponseBody
    public String send(@RequestParam(value = "userID",required = false) String userID,
                       @RequestParam(value = "deptID",required = false) String deptID,
                       String content) throws Exception {

        System.out.println("userID =" + userID);
        System.out.println("deptID =" + deptID);

        String accessToken = getAccessToken(weChatProperty.getCorpsecrets().get("message"));

        String url = weChatProperty.getUrls().get("send")+accessToken;

        HashMap<String, Object> map = new HashMap<>();

        if(StringUtils.isEmpty(deptID)){

            map.put("touser",userID);
        }

        if(StringUtils.isEmpty(userID)){

            map.put("toparty",deptID);
        }

        map.put("msgtype","text");

        HashMap<String, String> hashMap = new HashMap<>();

        hashMap.put("content",content);

        map.put("text",hashMap);

        map.put("agentid",weChatProperty.getAgentids().get("message"));


        ObjectMapper mapper = new ObjectMapper();

        String json = mapper.writeValueAsString(map);

        String result = HttpUtil.postJson(url,json);

        System.out.println("result = " + result);

        return result;
    }


    /**
     * 网页授权认证
     * @param code
     * @param model
     * @return
     * @throws IOException
     */
    @RequestMapping("/")
    public String getUserId(String code, Model model) throws IOException {

        System.out.println("code=" + code);


        if(!StringUtils.isEmpty(code)){

            String url = weChatProperty.getUrls().get("getuserinfo");

            HashMap<String, String> map = new HashMap<>();

            String accessToken = getAccessToken(weChatProperty.getCorpsecrets().get("message"));

            map.put("access_token",accessToken);
            map.put("code",code);

            String result = HttpUtil.get(url, map);

            model.addAttribute("result",result);

        }else{
            model.addAttribute("result","ERROR");
        }


        return "login";
    }


    @RequestMapping("/index")
    public String index(){

        return "index";
    }

}
